package com.tiancheng.onecode.trade;

import com.tiancheng.onecode.model.dto.PayMerchantDTO;
import com.tiancheng.onecode.model.dto.PayPlatformDTO;
import com.tiancheng.onecode.model.response.ApplicationAccessTokenResponseVO;
import com.tiancheng.onecode.utils.JsonUtil;
import com.tiancheng.onecode.utils.SpringContextHolder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Optional;

/**
 * @Author: kellen
 * @Description: 交易缓存组件
 * @Date: create in 2024/12/9 17:03
 */
@Data
public class TradeCache {

    /**
     * 应用授权缓存
     */
    public static final String APP_ACCESS_KEY="app:access:";
    public static final String MINI_ACCESS_TOKEN="mini:access:";
    public static final String TRADE_MERCHANT_INFO="trade:merchant:";
    public static final String TRADE_PLATFORM_INFO="trade:platform:";
    public static final String TRADE_MERCHANT_ACCOUNT_VERIFICATION_REG="trade:merchantReg:";

    private static final class RedissonClientHolder {
        private static final RedissonClient redissonClient = SpringContextHolder.getBean(RedissonClient.class);
    }

    private static RedissonClient getRedissonClient() {
        return RedissonClientHolder.redissonClient;
    }


    /**
     * 设置缓存的应用accessToken
     *
     * @param clientID id
     * @param accessToken token
     */
    public static void setAccessToken(String clientID,ApplicationAccessTokenResponseVO accessToken) {
        String key = APP_ACCESS_KEY + clientID;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        bucket.set(JsonUtil.toJsonString(accessToken), Duration.of(accessToken.getExpiresIn(), ChronoUnit.SECONDS));
    }

    /**
     *获取 缓存的应用accessToken
     *
     * @param clientID
     * @return
     */
    public static Optional<ApplicationAccessTokenResponseVO> getAccessToken(String clientID) {
        String key = APP_ACCESS_KEY + clientID;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        final String accessToken = bucket.get();
        if(StringUtils.isNotEmpty(accessToken)){
            return Optional.ofNullable(JsonUtil.fromJson(accessToken,ApplicationAccessTokenResponseVO.class));
        }
        return Optional.empty();
    }


    /**
     *删除 缓存的应用accessToken
     *
     * @param clientID
     * @return
     */
    public static Boolean delAccessToken(String clientID) {
        String key = APP_ACCESS_KEY + clientID;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        final String accessToken = bucket.get();
        if(StringUtils.isNotEmpty(accessToken)){
            return bucket.delete();
        }
        return false;
    }


    /**
     * 设置缓存小程序的accessToken
     *
     * @param clientID id
     * @param accessToken token
     */
    public static void cacheWechatAccessToken(String clientID,String accessToken,long expiresIn) {
        String key = MINI_ACCESS_TOKEN + clientID;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        bucket.set(accessToken, Duration.of(expiresIn, ChronoUnit.SECONDS));
    }
    /**
     * 设置缓存小程序的accessToken
     *
     * @param clientID id
     */
    public static String getWechatAccessToken(String clientID) {
        String key = MINI_ACCESS_TOKEN + clientID;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        final String accessToken = bucket.get();
        if(StringUtils.isNotEmpty(accessToken)){
            return bucket.get();
        }
        return null;
    }

    //-----------------------------------------------------------------交易商户缓存设置
    /**
     * 设置缓存交易商户信息
     *
     * @param platformMerchantId 平台商户id
     * @param payMerchantDTO 商户信息
     */
    public static void cacheTreadMerchant(Long platformMerchantId, PayMerchantDTO payMerchantDTO) {
        String key = TRADE_MERCHANT_INFO + platformMerchantId;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        bucket.set(JsonUtil.toJsonString(payMerchantDTO), Duration.of(48, ChronoUnit.HOURS));
    }
    /**
     * 获取缓存交易商户信息
     *
     * @param platformMerchantId id
     */
    public static Optional<PayMerchantDTO> getTreadMerchant(Long platformMerchantId) {
        String key = TRADE_MERCHANT_INFO + platformMerchantId;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        final String merchantStr = bucket.get();
        if(StringUtils.isNotEmpty(merchantStr)){
            return Optional.ofNullable(JsonUtil.fromJson(merchantStr,PayMerchantDTO.class));
        }
        return Optional.empty();
    }

    public static void  removeTreadMerchant(Long platformMerchantId){
        String key = TRADE_MERCHANT_INFO + platformMerchantId;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        if(bucket.isExists()){
            bucket.delete();
        }
    }

    /**
     * 缓存对公账户进件申请，4小时
     *
     * @param platformMerchantId 平台商户id
     * @param tradeMerchantPayRegId 交易中台商户进件id
     */
    public static void cacheTreadMerchantAccountVerification(Long platformMerchantId, Long tradeMerchantPayRegId) {
        String key = TRADE_MERCHANT_ACCOUNT_VERIFICATION_REG + platformMerchantId;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        bucket.set(tradeMerchantPayRegId.toString(), Duration.of(4, ChronoUnit.HOURS));
    }
    /**
     * 获取缓存对公账户进件申请id
     *
     * @param platformMerchantId id
     */
    public static Optional<Long> getTreadMerchantAccountVerification(Long platformMerchantId) {
        String key = TRADE_MERCHANT_ACCOUNT_VERIFICATION_REG + platformMerchantId;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        final String tradeMerchantPayRegId = bucket.get();
        if(StringUtils.isNotEmpty(tradeMerchantPayRegId)){
            return Optional.of(Long.valueOf(tradeMerchantPayRegId));
        }
        return Optional.empty();
    }
    /**
     * 删除缓存对公账户进件申请id
     *
     * @param platformMerchantId id
     */
    public static void removeTreadMerchantAccountVerification(Long platformMerchantId) {
        String key = TRADE_MERCHANT_ACCOUNT_VERIFICATION_REG + platformMerchantId;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        if(bucket.isExists()){
            bucket.delete();
        }
    }



    //-----------------------------------------------------------------平台缓存设置
    /**
     * 设置缓存交易平台信息
     *
     * @param payPlatformDTO 平台
     */
    public static void cacheTreadPlatform(String platformCode,PayPlatformDTO payPlatformDTO) {
        String key = TRADE_PLATFORM_INFO + platformCode;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        bucket.set(JsonUtil.toJsonString(payPlatformDTO), Duration.of(7, ChronoUnit.DAYS));
    }
    /**
     * 获取缓存交易平台信息
     */
    public static Optional<PayPlatformDTO> getTreadPlatform(String platformCode) {
        String key = TRADE_PLATFORM_INFO + platformCode;
        RBucket<String> bucket = getRedissonClient().getBucket(key);
        final String platformStr = bucket.get();
        if(StringUtils.isNotEmpty(platformStr)){
            return Optional.ofNullable(JsonUtil.fromJson(platformStr,PayPlatformDTO.class));
        }
        return Optional.empty();
    }


}
